package net.xmat.webtest.web.controller;

import java.util.List;

import net.xmat.webtest.beans.Author;
import net.xmat.webtest.dao.IAuthorDao;
import net.xmat.webtest.dao.impl.HibernateTransaction;
import net.xmat.webtest.dao.impl.IHibernateWork;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/authors")
public class AuthorController {

	@Autowired
	private IAuthorDao authorDao;
	
	@RequestMapping(value="/list", method = RequestMethod.GET)
	public String list(String name, ModelMap modelMap) {
		List<Author> authors = authorDao.list(name);
		modelMap.addAttribute("authors", authors);
		return "/authors";
	}

	@RequestMapping(value="/save", method = RequestMethod.POST)	
	public String add(final Author a) {
		new HibernateTransaction().execute(
			new IHibernateWork() {
				public void doInTransaction(Session session) {
					session.saveOrUpdate(a);
				}
			}
		);
		// redirect to /list url
		return "redirect:list";
	}
	
	@RequestMapping(value="/load", method = RequestMethod.GET)	
	public void load() {
		// redirect to /list url 
	}
	
	
}
